package com.System.LearnSystemStudent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.System.LearnSystem.Result;
import com.System.LearnSystem.Token;
import com.System.LearnSystem.sqlConnection;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

class Message2 //用于临时储存客户端发来的请求信息
{
	protected String token;
	protected String role;
	protected String username;
	
	public Message2()
	{
		
	}
	
	public String gettoken()
	{
		return token;
	}
	public void settoken(String token)
	{
		this.token = token;
	}
	
	public String getrole()
	{
		return role;
	}
	public void setrole(String role)
	{
		this.role = role;
	}
	
	public String getusername()
	{
		return username;
	}
	public void setusername(String username)
	{
		this.username = username;
	}
}

@WebServlet("/stu/searchSchedule")
public class searchSchedule extends HttpServlet {
	
	public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException
	{
		StringBuilder json = new StringBuilder();//读取json请求
        BufferedReader reader = req.getReader();
        String line;
        while ((line = reader.readLine()) != null) {
            json.append(line);
        }
 
        String jsonStr  = json.toString();            
        ObjectMapper objectMapper = new ObjectMapper();
        Message2 message = objectMapper.readValue(jsonStr, Message2.class);
        
        objectMapper.registerModule(new JavaTimeModule());//注册驱动保证序列化
        res.setContentType("application/json;charset=UTF-8");//设置响应格式
        PrintWriter out = res.getWriter();
        
        if(message.gettoken()!=null)
        {
        	if(Token.checkToken(message.gettoken())&&"STUDENT".equals(message.getrole()))//验证令牌和身份
        	{
        		String sql = "SELECT * FROM "+message.getusername();//访问课表的语句
        		try (Connection conn = sqlConnection.getConnection();Statement statement = conn.createStatement())
        		{
        			ResultSet rs = statement.executeQuery(sql);
        			int n = 0;
        			Schedule schedule = new Schedule(); 
        			while(rs.next()) //读取每天的课表
        			{
        				schedule.setlesson(n,1,rs.getString("FIRST"));
        				schedule.setlesson(n,2,rs.getString("Second"));
        				schedule.setlesson(n,3,rs.getString("M"));
        				schedule.setlesson(n,4,rs.getString("Third"));
        				schedule.setlesson(n,5,rs.getString("N"));
        				schedule.setlesson(n,6,rs.getString("Fourth"));
        				schedule.setlesson(n,7,rs.getString("Fifth"));
        				n++;
        			}
        			Result<Schedule> result = Result.success(schedule);
        			out.println(objectMapper.writeValueAsString(result));
        		} catch (SQLException e) {
        			System.err.println("SQL 错误码: " + e.getErrorCode());
          		  	System.err.println("SQL 错误消息: " + e.getMessage());
          		    e.printStackTrace();
				}
        	}
        	else
        	{
        		Result<Object> result = Result.error(401,"非授权访问");
        		out.println(objectMapper.writeValueAsString(result));
        	}
        }
        else
        {
        	Result<Object> result = Result.error(401,"非授权访问");
        	out.println(objectMapper.writeValueAsString(result));
        }
	}

}

class Schedule //用于存储课程表
{
	private String[][] lesson;
	
	public Schedule()
	{
		lesson = new String[7][8];
		lesson[0][0] = "周一";
		lesson[1][0] = "周二";
		lesson[2][0] = "周三";
		lesson[3][0] = "周四";
		lesson[4][0] = "周五";
		lesson[5][0] = "周六";
		lesson[6][0] = "周日";
	}

	public void setlesson(int a,int b,String lesson)
	{
		this.lesson[a][b] = lesson;
	}
	public String[][] getlesson()
	{
		return lesson;
	}
	
}




